package org.example.com.Controller;

import com.alibaba.fastjson2.JSONArray;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;

import org.example.com.Bean.Alipay;
import org.example.com.Bean.Order;
import org.example.com.config.JwtConfig;
import org.example.com.service.OrderService;
import org.example.com.service.SeckillServcice;
import org.example.com.service.ShoppingCartService;
import org.example.com.util.JsonResult;
import org.example.com.util.RecultTool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
@RestController
@RequestMapping("/alipay")
public class AlipayController {
    @Value("${appId}")
    private String appId;
    @Value("${privateKey}")
    private String privateKey;
    @Value("${publicKey}")
    private String publicKey;
    @Value("${notifyUrl}")
    private String notifyUrl;
    @Value("${returnUrl}")
    private String returnUrl;
    @Value("${signType}")
    private String signType;
    @Value("${charset}")
    private String charset;
    @Value("${gatewayUrl}")
    private String gatewayUrl;
    @Resource
    private ShoppingCartService shoppingCartService;
    @Resource
    private OrderService orderService;

    @RequestMapping("/pay")
    @ResponseBody
    public String pay(Alipay alipayBean) throws AlipayApiException {
        this.total_amount=alipayBean.getTotal_amount();
        this.orderId= new Date().getTime()+""+(int)(Math.random()*100000);
        alipayBean.setOut_trade_no(this.orderId);
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, appId, privateKey,"json", charset,publicKey, signType);

        AlipayTradePagePayRequest alipayTradeAppPayRequest = new AlipayTradePagePayRequest();
        System.out.printf("进入了这里"+alipayBean);
        // 设置付款成功后应跳转的路径
        alipayTradeAppPayRequest.setNotifyUrl(notifyUrl);
        alipayTradeAppPayRequest.setReturnUrl(returnUrl);
        // 设置支付宝的各项参数
        String json = JSONArray.toJSONString(alipayBean);
        alipayTradeAppPayRequest.setBizContent(json);
        System.out.println("支付的参数是：" + json);
        System.out.println(JSONArray.toJSONString(alipayTradeAppPayRequest));
        // 生成最终的订单
        return alipayClient.pageExecute(alipayTradeAppPayRequest).getBody();
    }
    private String token;
    private String[] ids;
    private String total_amount;
    private String orderId;
    @GetMapping("/get_data")
    @ResponseBody
    public JsonResult getData(HttpServletRequest request, @RequestParam("ids") String[] ids){
        this.ids=ids;
        this.token=request.getHeader("token");
        return RecultTool.success("success");
    }
    @GetMapping("/success")
    public String success(){
        Order order=new Order();
        String userId= JwtConfig.parseJWT(token).get("id").toString();
        order.setId(orderId);
        order.setUserId(userId);
        order.setTotalAmount(total_amount);
        order.setTotalCount(ids.length+"");
        orderService.saveOrder(order,ids);
        // 2. 删除已购买商品的购物车
        shoppingCartService.batchRemoveCate(token,ids);
        //跳转到首页
        return "redirect:http://localhost:8082/front/index.html";




    }

}
